﻿using System;
using System.Linq;
using Microsoft.Practices.Unity;
using MyCompany.WebCrawler.Domain;
using MyCompany.WebCrawler.Interfaces;
using MyCompany.WebCrawler.Repositories;
using MyCompany.WebCrawler.Tasks;

namespace WebCrawler
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Count() == 1)
            {
                CrawlPage(args[0]);
            }
            else
            {
                ShowHelp();
            }

            Console.WriteLine("");
            Console.WriteLine("Press any key....");
            Console.ReadKey();
        }

        private static void CrawlPage(string url)
        {
            var crawler = CreateCrawler();
            crawler.RunMultithread(url);
        }

        private static Crawler CreateCrawler()
        {
            var c = new UnityContainer();

            c.RegisterInstance<IDownloader>(new Downloader());
            c.RegisterInstance<IUiFetcher>(new UiFetcher());
            c.RegisterInstance<IRepository>(new Repository());
            c.RegisterType<TasksFactory>();
            c.RegisterType<Crawler>();

            return c.Resolve<Crawler>();
        }

        private static void ShowHelp()
        {
            Console.WriteLine("Example: crawler.exe url");
        }


    }
}
